/*
 * connection_manager.h
 *
 *  Created on: 2021年2月2日
 *      Author: kimfeng
 */

#ifndef CONNECTION_MANAGER_H_
#define CONNECTION_MANAGER_H_

#include "connection.h"

#include <set>
#include <boost/thread/mutex.hpp>
#include <boost/asio.hpp>

class connection_manager
{
public:
	explicit connection_manager(boost::asio::io_service& io_svc);
	virtual ~connection_manager();

	void start(connection_ptr c);
	void stop (connection_ptr c);

	connection_id_t get_connection_id();

	connection_ptr get_connection(connection_id_t id);

protected:
	virtual void send_heartbeat();

private:
	std::map<connection_id_t, connection_ptr> m_connections;
	boost::mutex m_mutex;
	boost::asio::deadline_timer m_timer;

	void check_alive();
	void handle_timeout();

	connection_id_t m_last_id;
};

#endif /* CONNECTION_MANAGER_H_ */
